home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / TAWUG / TAWUG Disk No. 73 (SHK) / TAWUG73.shk / ASSEMBLY.LANG.4 (.txt) next >
AppleWorks Document  |  1988-07-12  |  15KB  |  231 lines

  1. O=====|====|====|====|====|====|====|====|====|====|====|====|====|====|====|===
  2. Learning Apple 65C02 Assembly Language - Session four
  3. Author: Mark Niemann-Ross
  4.         1423 Holly 
  5.         Denver, Colorado 80220
  6. JYou may download and distribute this to any outlet via any method, please H
  7. Fjust spread the word about what a swell guy I am and how hard I work.     
  8. Thanks.
  9. 65C02 COMMANDS&
  10. Covers: 65C02 commands and functions
  11. /|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\- /|\
  12. GHowdy, howdy, howdy. Big greetings from another Western Computer nerd! M
  13. KLooks like it's time to stop procrastinating and send out the next missile 
  14. in the programming wars.
  15. First, take a big breather and review where we've been so far: 
  16. 1) Reasons for doing this jive
  17. 2) Numbering Systems (Hex, Binary and Decimal)
  18. 3) 65C02 internalsD
  19. 4) a special bonus edition on branching (A little out of sequence)
  20. II would appreciate it if you would capture this series of questions, and J
  21. Hpost the answers, or, if you're shy, send them to me via e-mail on this I
  22. Gsystem. In fact, let's make this a little test. Oh, by the way, If you L
  23. Janswer all my requests for feedback, I'll be sure to mail you a bona-fide M
  24. Kcertificate of achievement, suitable for framing and posting on your wall. L
  25. JHurry, send your answers in today, don't be the last kid on your block to *
  26. receive this life-time offer! Here goes:
  27. 1) Describe a register found in the 65C02 and its functions.D
  28. 2) Why would anybody in their right mind want to use Hex notation?K
  29. I3) Diagram a hierarchy of interfaces found on the Apple //e, //c, ][, or 
  30. 4) Come up with an obscure piece of trivia concerning the 65C02
  31. FOk...that's it. I'll be waiting for your answers. Prizes for the most 
  32. entertaining answers.
  33. KNow, as Bugs Bunny used to say, on with the show. I'm going to pontificate J
  34. on the command set found in the 65C02. You will also need to know about 
  35. Bsomething called "addressing modes" before you can begin to write J
  36. Hassembly, but that can wait till next session. I'm hoping that when you '
  37. are done with this session, you will:
  38. 1) Describe a mnemonic and its operation when seen.F
  39. D2) Know where to find out about that mnemonics action on the status 
  40. register.&
  41. 3) Know what the status register is!
  42. JFollowing this discussion is a blow by blow description of operations and M
  43. Kflag conditions. You might also want to download the associated appleworks J
  44. Hfile containing the 65C02 opcodes. (Apologies to non-appleworks users.) K
  45. IThis data base contains each opcode, its associated addressing modes and K
  46. Iits effect on the status register. It is intended to be a reference, and *
  47. will make more sense after next session.
  48. THE DISCUSSION BEGINS:
  49. IAssembly language is no different than other languages in regards to the M
  50. Kfact that it uses commands to accomplish set tasks. One command, one task. J
  51. HThe difference between languages is syntax and structure. Once learned, .
  52. one language becomes amazingly like another.
  53. SYNTAXL
  54. JAssembly language is thought of as difficult because so much accomplishes H
  55. Fso little. "High Level" languages use a kind of shorthand to simplify K
  56. Ioften used tasks. (i.e. BASIC uses "PRINT" to send a string to an output K
  57. Idevice.) Assembly language, in its un-embellished form, doesn't have any L
  58. Jshort cuts such as these. You can tell BASIC to go buy groceries, but you J
  59. Hhave to tell assm to put on its jacket, open the door, walk through the I
  60. Gdoor, close the door, walk to the grocery store, get a cart, go to the J
  61. Hproduce section, select a 2-pound bag of carrots, place it in the cart, M
  62. Kand so on and so on. Asm deals with one value at a time. Syntax is kind of I
  63. Ga grunt and sweat sort of thing, and that section appears last in this H
  64. Fsection. Each command is identified and described. (Hope you got your 
  65. capture buffer on!)
  66. STRUCTUREK
  67. IA command in assembly language has three parts. The opcode (or command), J
  68. Hthe argument, and THE ALL IMPORTANT COMMENT!!!! On paper, it looks very E
  69. Ctabular, neatly stacked columns, one line dealing with one byte of I
  70. Ginformation. By the way, if you look at an assm listing right now, you F
  71. Dwill see that there are actually eight or so columns. The three I'm L
  72. Jdiscussing right now are the most important, and everything else revolves 
  73. around them.
  74. Here's a simple assm command line:
  75. LDA  #$8D       ;place the ascii code for return in accumulator
  76. K   "LDA" is the opcode, or command. It tells the 65C02 what it is going to I
  77. Gdo. LDA is actually called a "mnemonic" (defined as a device to aid in I
  78. Gremembering something.) LDA is assm for "Load the Accumulator with the 
  79. following value."L
  80. J   "#$8D" should look familiar by now. It is hexadecimal notation for the K
  81. Idecimal value 141, or the ascii representation for a carriage return. If M
  82. Kyou feed a display an $8D, it should place the cursor at the left edge and K
  83. Idrop down a line of text. The number sign (#) is notation that tells the D
  84. Bassembler that this is to be treated as a quantity, rather than a M
  85. Klocation. This distinction is obviously very important, and all assemblers J
  86. Hwill have some method of signifying this distinction, although some use 
  87. different ways to do it. L
  88. J   The third line is a comment. It starts with a semi-colon (;), which is M
  89. Khow the assembler knows that this is just for human eyes (exactly like the C
  90. ABASIC "REM" statement.) Again, different assemblers will do this J
  91. Hdifferently, but if you ever run into one that does not allow comments, M
  92. Kwhoever wrote it committed a crime and whoever bought it should be pitied. H
  93. FIF YOU EVER SEND ME A CHUNK OF ASSEMBLY CODE WITHOUT COMMENTS, I WILL H
  94. QUESTION YOUR ANCESTRY AND DEMOTE YOU TO POLISHER OF THE SLIDE RULES! 
  95. CInstead of me wasting our collective time ranting and raving about K
  96. Icomments, I would suggest you read any book on programming and take your 
  97. tips there.
  98. JSo. Each line in an assm program describes an action, then describes what F
  99. Dis being acted upon. It's that simple. Look at this program, paying <
  100. special attention to identifying these three major parts. 
  101. 1) Opcode (mnemonic)
  102. 2) Argument
  103. 3) Comment
  104.                 1    ********************************C
  105.                 2    * This is a program that converts keystrokes%
  106.                 3    * Into sounds.7
  107.                 4    ********************************7
  108.                 5    *-- Equates --------------------L
  109.                 6    KEY      EQU   $C000      ;Location of keyboard inputJ
  110.                 7    CLRKEY   EQU   $C010      ;Switch to clear keyboardE
  111.                 8    SPKR     EQU   $C030      ;Location of speaker7
  112.                 9    *-------------------------------
  113.                 10B
  114.                 11            ORG   $8000      ;"Bload" at $80007
  115.                 12   *-- The Program ----------------
  116. 8000: 2C 10 C0  13   START    BIT   CLRKEY     ;Clear the keyboardA
  117. 8003: AD 00 C0  14   KEYLOOP  LDA   KEY        ;check for inputK
  118. 8006: 10 FB     15            BPL   KEYLOOP    ;IF none THEN GOTO KEYLOOPF
  119. 8008: A0 FF     16            LDY   #$FF       ;Got One! Let Y = 256E
  120. 800A: 2C 30 C0  17   SPEAK    BIT   SPKR       ;"Click" the speakerF
  121. 800D: AA        18            TAX              ;X = keypress (Delay);
  122. 800E: CA        19   WAIT     DEX              ;X = X - 1D
  123. 800F: D0 FD     20            BNE   WAIT       ;IF X > 0 THEN WAIT;
  124. 8011: 88        21            DEY              ;Y = Y - 1E
  125. 8012: D0 F6     22            BNE   SPEAK      ;IF Y > 0 THEN SPEAKE
  126. 8014: F0 EA     23            BEQ   START      ;IF Y = 0 THEN START
  127. (Here's a hint! ------------ Opcode Argument   Comment)
  128. AWhat is shown above is a nice printout of the program when it is L
  129. Jassembled.  It has lots of extra information that the assembler provides, K
  130. Ias well as the information that we have fed the assembler to produce the J
  131. Hprogram. Below is the raw listing of the program, without any tabs. You H
  132. Fshould be able to feed this into your assembler and, depending on the M
  133. Kdifferent ways that individual assemblers do things, your assembler should L
  134. produce a printout similar to the above, as well as an executable program.K
  135. ITry it.  If it doesn't work, tell me about it and we'll try to figure it 
  136. ********************************3
  137. * This here is a program that converts keystrokes
  138. * Into sounds."
  139. ********************************"
  140. *-- Equates --------------------+
  141. KEY EQU $C000 ;Location of keyboard input,
  142. CLRKEY EQU $C010 ;Switch to clear keyboard%
  143. SPKR EQU $C030 ;Location of speaker"
  144. *-------------------------------
  145.  ORG $8000 ;"Bload" at $8000"
  146. *-- The Program ----------------&
  147. START BIT CLRKEY ;Clear the keyboard/
  148. KEYLOOP LDA KEY ;check the keyboard for input,
  149.  BPL KEYLOOP ;IF no key, THEN GOTO KEYLOOP!
  150.  LDY #$FF ;Got One! Let Y = 256%
  151. SPEAK BIT SPKR ;"Click" the speaker
  152.  TAX ;X = keypress (Delay)
  153. WAIT DEX ;X = X - 1
  154.  BNE WAIT ;IF X > 0 THEN WAIT
  155.  DEY ;Y = Y - 1!
  156.  BNE SPEAK ;IF Y > 0 THEN SPEAK!
  157.  BEQ START ;IF Y = 0 THEN START
  158. IAfter seeing the difference, remember this. You will type the program as K
  159. Iit appears in the second listing. Hopefully, your assembler will provide J
  160. Htabs and columns to pretty things up a bit, but you do not have to type J
  161. Hthings like line numbers, or the columns of hex digits appearing in the 2
  162. first listing. That is the job of the assembler.
  163. FAt this point, examine the program. Try and piece together what it is G
  164. Edoing, and what commands mean what. In future sessions, we will give L
  165. Jblow-by-blow descriptions of what is going on, but for now this is a good 
  166. learning experience.
  167. Onwards...G
  168. EThis is a good time to flesh out the concept of the status register. C
  169. ABasically, it is a set of indicators showing the results of some I
  170. Goperation. Anytime the 65C02 makes a decision, it does so based on the K
  171. Icontents of the status register. (You will also hear the status register 1
  172. referred to as "flags.") There are seven flags:
  173. I1) N - The Negative flag. Set to 1 when an operation produces a negative H
  174. F(8th bit hi) number. Recall that in hex, negative numbers have the hi G
  175. Ebit set. $8F is considered "negative," $0F is considered "positive." A
  176. This flag is useful to test the condition of a switch or port.     
  177. BOftentimes, the hardware designer will have tied the "data ready" I
  178. Gsignal to the hi bit. If there is data ready, the hi bit will equal 1. H
  179. FIf there isn't, the hi bit will equal 0. The Apple reads the keyboard J
  180. Husing this scheme. Notice lines 14 and 15 in the above program. Line 14 F
  181. Dloads the accumulator with the value at the keyboard entry location E
  182. C($C000). In the act of loading, the N flag is set according to the H
  183. Fvalue of the hi bit of the keyboard value. If a key has been pressed, J
  184. Hthe hi bit will equal 1. If no keypress, it will equal 0. Line 15 tests E
  185. Cthis with BPL, or Branch if Plus. If the hi bit is 0, indicating a I
  186. Gpositive number (and no keypress), the program will branch (GOTO) line I
  187. G14, labeled KEYLOOP. Over and over this happens until a key is pressed J
  188. Hand the hi bit turns to 1. "BPL," finding that the N flag is indicating G
  189. Ethat a negative number has been read, does not branch and control is 
  190. passed on to line 16.M
  191. K2) V - oVerflow. This mysterious little flag has to do with the results of J
  192. Hsigned arithmetic and something called "twos complements." Let's not go 
  193. into it right now.  M
  194. K3) B - Break. Shows whether a break has occurred. Doubtful if you will use 
  195. it for some time. (If ever)D
  196. 4) D - Decimal. Another good thing not to mess with at this point.<
  197. 5) I - Interrupt. Indicates if an interrupt has occurred. L
  198. J6) Z - Zero. Boy oh boy is this useful. Anytime an operation results in a F
  199. Dvalue of zero, this flag lights up. Actually, this flag functions a G
  200. little bit backwards from what you would expect. If the result of an 
  201. ?operation is equal to zero, the Z flag will become equal to 1. I
  202. GConversely, if the result of an operation is not zero, the Z flag will I
  203. Gbe equal to 0. Instead of thinking in terms of 1 and 0, think in terms I
  204. Gof true (1) and false (0). In real life, you don't have to worry about F
  205. Dthis, as the instructions that use it (BNE and BEQ) behave like you D
  206. Bexpect them to. BNE stands for Branch if Not Equal (to zero). BEQ I
  207. Gstands for Branch if Equal (to zero). Notice in our program above that D
  208. Bwe use BNE a couple of times to test the condition of the X and Y H
  209. Fregisters. In line 19, the X register is decremented. We then test to J
  210. Hsee if the result of subtracting one from X is equal to zero (BNE WAIT, F
  211. Dline 20). If the result is NOT equal, then we loop back to the line H
  212. Flabeled WAIT (line 19) and repeat the process again until it is. This H
  213. Fproduces a delay, which affects the pitch of the tone produced. The Z G
  214. Eflag can also be used to compare to values to determine whether they J
  215. Hare equal or not equal. This involves using the CMP opcode, followed by 
  216. a BNE or BEQ. More later.J
  217. H7) C - Carry. Ahh, the beloved C flag. This little bugger gets more use G
  218. Ethan a fireplug at a dog show. There are instructions to voluntarily J
  219. Hclear the c flag (CLC - Sets C = 0), set the c flag (SEC - Sets C = 1), G
  220. Eas well as branch on C set (BCS) or clear (BCC). It is often used to B
  221. @pass results from a subroutine to a calling program, as well as I
  222. Gtemporary storage of one bit of information. Also, take a look at LSR, H
  223. ASL, ROL, and ROR. With these commands and the C flag you can examine 
  224. Hand modify a byte one bit at a time. EXTREMELY useful when dealing with #
  225. the Apple hires graphics screens.
  226. KHOLY SMOKES....That's enough for this file! BUT...don't lose it. I'm going M
  227. Kto refer to the program above in the next session. In fact, it might be to J
  228. Hyour advantage to have a printed copy of it available next time around. !
  229. AND DON'T FORGET YOUR HOMEWORK!
  230. Until then...
  231.